package cn.com.wxd.service.javamail;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.activation.FileDataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by ComXu on 2017/5/18.
 */
public class AsyncMailSender extends MailSender{
    private static final Logger log = LoggerFactory.getLogger(AsyncMailSender.class);

    private static final ExecutorService threadPool = Executors.newCachedThreadPool();
    /**
     * 默认从配置文件中创建实例
     */
    public AsyncMailSender(){
        super();
    }
    /**
     * 自定义用户创建实例
     * @param smtpHostName smtp值
     * @param username 默认发件邮箱
     * @param password 密码
     * @param port 端口
     */
    public AsyncMailSender(String smtpHostName,String username,String password,String port){
        super(smtpHostName, username, password, port);
    }
    @Override
    /**
     * 发送邮件
     *
     * @param recipient 收件人邮箱地址
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return 发送是否成功
     */
    public boolean send(final String recipient, final String sunject, final String content){
        try {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    AsyncMailSender.super.send(recipient,sunject,content);
                }
            };
            submintThread(runnable);
            return true;
        }catch (Exception e){
            log.error("邮件发送失败!",e);
            return false;
        }
    }
    /**
     * 群发邮件
     *
     * @param recipients 收件人列表
     * @param subject 主题
     * @param content 内容
     * @return 是否成功
     */
    public boolean send(final List<String> recipients, final String subject, final String content){
        try {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    AsyncMailSender.super.send(recipients,subject,content);
                }
            };
            submintThread(runnable);
            return true;
        }catch (Exception e){
            log.error("邮件发送失败!",e);
            return false;
        }
    }
    /**
     * 发送带图片的邮件
     *
     * @param recipients 收件人列表
     * @param subject 主题
     * @param content 内容
     * @param img 图片数据源
     * @return 是否成功
     */
    public boolean send(final List<String> recipients, final String subject, final String content, final FileDataSource img){
        try {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    AsyncMailSender.super.send(recipients,subject,content,img);
                }
            };
            submintThread(runnable);
            return true;
        }catch (Exception e){
            log.error("邮件发送失败!",e);
            return false;
        }
    }
    /**
     * 发送带附件的邮件
     *
     * @param recipients 收件人列表
     * @param subject 主题
     * @param content 内容
     * @param img 图片数据源
     * @return 是否成功
     */
    public boolean send(final List<String> recipients, final String subject, final FileDataSource file, final String content){
        try {
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    AsyncMailSender.super.send(recipients,subject,file,content);
                }
            };
            submintThread(runnable);
            return true;
        }catch (Exception e){
            log.error("邮件发送失败!",e);
            return false;
        }
    }

    private void submintThread(Runnable runnable) throws Exception{
        try {
            threadPool.submit(runnable);
        }catch (Exception e){
            throw e;
        }
    }
}
